/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.teach.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.modules.teach.entity.TeachDistributePaper;
import com.lczyfz.istep.modules.teach.service.TeachDistributePaperService;

/**
 * 下发_试卷中间表Controller
 * @author cqh
 * @version 2017-05-19
 */
@Controller
@RequestMapping(value = "${adminPath}/teach/teachDistributePaper")
public class TeachDistributePaperController extends BaseController {

	@Autowired
	private TeachDistributePaperService teachDistributePaperService;
	
	@ModelAttribute
	public TeachDistributePaper get(@RequestParam(required=false) String id) {
		TeachDistributePaper entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = teachDistributePaperService.get(id);
		}
		if (entity == null){
			entity = new TeachDistributePaper();
		}
		return entity;
	}
	
	@RequiresPermissions("teach:teachDistributePaper:view")
	@RequestMapping(value = {"list", ""})
	public String list(TeachDistributePaper teachDistributePaper, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TeachDistributePaper> page = teachDistributePaperService.findPage(new Page<TeachDistributePaper>(request, response), teachDistributePaper); 
		model.addAttribute("page", page);
		return "modules/teach/teachDistributePaperList";
	}

	@RequiresPermissions("teach:teachDistributePaper:view")
	@RequestMapping(value = "form")
	public String form(TeachDistributePaper teachDistributePaper, Model model) {
		model.addAttribute("teachDistributePaper", teachDistributePaper);
		return "modules/teach/teachDistributePaperForm";
	}

	@RequiresPermissions("teach:teachDistributePaper:edit")
	@RequestMapping(value = "save")
	public String save(TeachDistributePaper teachDistributePaper, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, teachDistributePaper)){
			return form(teachDistributePaper, model);
		}
		teachDistributePaperService.save(teachDistributePaper);
		addMessage(redirectAttributes, "保存下发_试卷中间表成功");
		return "redirect:"+Global.getAdminPath()+"/teach/teachDistributePaper/?repage";
	}
	
	@RequiresPermissions("teach:teachDistributePaper:edit")
	@RequestMapping(value = "delete")
	public String delete(TeachDistributePaper teachDistributePaper, RedirectAttributes redirectAttributes) {
		teachDistributePaperService.delete(teachDistributePaper);
		addMessage(redirectAttributes, "删除下发_试卷中间表成功");
		return "redirect:"+Global.getAdminPath()+"/teach/teachDistributePaper/?repage";
	}

}